import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_app/models/community.dart';
import 'package:flutter_app/models/general_type.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_app/widgets/commom_action_button.dart';
import 'package:flutter_app/widgets/common_title.dart';
import 'package:flutter_app/widgets/form_item_field.dart';
import 'package:flutter_app/widgets/form_item_radio.dart';
import 'package:flutter_app/widgets/form_item_select.dart';
import 'package:flutter_app/widgets/image_picker.dart';
import 'package:flutter_app/pages/release_house/room_configuration.dart';
import 'package:image_picker/image_picker.dart';

/// @description: 发布房源页
class ReleaseHousePage extends StatefulWidget {
  const ReleaseHousePage({Key? key}) : super(key: key);

  @override
  State<ReleaseHousePage> createState() => _ReleaseHousePageState();
}

class _ReleaseHousePageState extends State<ReleaseHousePage> {
  int rentType = 0;
  int decorate = 0;
  int roomType = 0;
  int floorType = 0;
  int towardType = 0;

  List<GeneralType> floorList = [];
  List<GeneralType> orientedList = [];
  List<GeneralType> roomTypeList = [];

  var titleController = TextEditingController();
  var descriptionController = TextEditingController();

  var community;

  _getParams() async {
    var res = await DioHttp.of(context).get('/houses/params');
    var resString = json.decode(res.toString())["body"];
    List<GeneralType> floorList = List<GeneralType>.from(
        resString["floor"].map((i) => GeneralType.fromJson(i)));
    List<GeneralType> orientedList = List<GeneralType>.from(
        resString["oriented"].map((i) => GeneralType.fromJson(i)));
    List<GeneralType> roomTypeList = List<GeneralType>.from(
        resString["roomType"].map((i) => GeneralType.fromJson(i)));
    setState(() {
      this.floorList = floorList;
      this.orientedList = orientedList;
      this.roomTypeList = roomTypeList;
    });
  }

  @override
  void initState() {
    _getParams();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: CommomActionButton(
        name: '提交',
        onPressed: () {
          // Navigator.pushNamed(context, '/releaseHouse');
        },
      ),
      appBar: AppBar(
        title: const Text('发布房源'),
      ),
      body: ListView(
        children: [
          const CommonTitle(
            title: '房源信息',
          ),
          CommonFormItemField(
            contentBuilder: (context) {
              return Expanded(
                  child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  var result = Navigator.pushNamed(context, 'selectCommunity');
                  result.then((value) {
                    if (null != value) {
                      setState(() {
                        community = value;
                      });
                      print({community, 'community'});
                    }
                  });
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      community?.name ?? '请选择小区',
                      style: const TextStyle(fontSize: 16),
                    ),
                    const Icon(Icons.keyboard_arrow_right)
                  ],
                ),
              ));
            },
            lable: '小区',
          ),
          const CommonFormItemField(
              lable: '租金', hintText: '请输入租金', suffixText: '元/月'),
          const CommonFormItemField(
              lable: '大小', hintText: '请输入房源大小', suffixText: '平方米'),
          FormItemRadio(
            options: const ['合租', '整租'],
            lable: '租赁方式',
            value: rentType,
            onChange: (index) {
              setState(() {
                rentType = index;
              });
            },
          ),
          FormItemRadio(
            options: const ['精装', '简装'],
            lable: '装修',
            value: decorate,
            onChange: (index) {
              setState(() {
                decorate = index;
              });
            },
          ),
          FormItemSelect(
            value: roomType,
            options: roomTypeList.map((e) => e.name).toList(),
            lable: '户型',
            onChange: (v) {
              setState(() {
                roomType = v;
              });
            },
          ),
          FormItemSelect(
            value: floorType,
            options: floorList.map((e) => e.name).toList(),
            lable: '楼层',
            onChange: (v) {
              setState(() {
                floorType = v;
              });
            },
          ),
          FormItemSelect(
            value: towardType,
            options: orientedList.map((e) => e.name).toList(),
            lable: '朝向',
            onChange: (v) {
              setState(() {
                towardType = v;
              });
            },
          ),
          const CommonTitle(
            title: '房屋图片',
          ),
          CommonImagePicker(
            onChange: (List<XFile> value) {},
          ),
          const CommonTitle(
            title: '房屋标题',
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: TextField(
              controller: titleController,
              decoration: const InputDecoration(
                  hintText: '请输入标题', border: InputBorder.none),
            ),
          ),
          const CommonTitle(
            title: '房屋配置',
          ),
          RoomConfiguration(
            onChange: (value) {},
          ),
          const CommonTitle(
            title: '房屋描述',
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: TextField(
              controller: descriptionController,
              decoration: const InputDecoration(
                  hintText: '请输入房屋描述信息', border: InputBorder.none),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(bottom: 100.0),
          )
        ],
      ),
    );
  }
}
